<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <!--    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">-->
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <!--    <script src="https://unpkg.com/element-ui/lib/index.js"></script>-->
    <script src="../js/axios-0.18.0.js"></script>
    <script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script>
    <script type="text/javascript"
            src="http://webapi.amap.com/maps?v=1.4.15&key=2cf5fcd8c3c0f969590834a513776a95&plugin=AMap.Autocomplete,AMap.PlaceSearch,AMap.Geocoder"></script>
    <script type="text/javascript" src="https://cache.amap.com/lbs/static/PlaceSearchRender.js"></script>
    <style>
        #container {
            width: 400px;
            height: 180px;
        }

        #container4edit {
            width: 400px;
            height: 180px;
        }

        .avatar-uploader .el-upload {
            border: 1px dashed #d9d9d9;
            border-radius: 6px;
            cursor: pointer;
            position: relative;
            overflow: hidden;
        }

        .avatar-uploader .el-upload:hover {
            border-color: #409EFF;
        }

        .avatar-uploader-icon {
            font-size: 28px;
            color: #8c939d;
            width: 178px;
            height: 178px;
            line-height: 178px;
            text-align: center;
        }

        .avatar {
            width: 178px;
            height: 178px;
            display: block;
        }

        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }

        .amap-icon img,
        .amap-marker-content img {
            width: 25px;
            height: 34px;
        }

        .amap-sug-result {
            z-index: 2999 !important;
        }

    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>体检机构管理<small>体检机构地址管理</small></h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>体检机构管理</el-breadcrumb-item>
            <el-breadcrumb-item>体检机构地址管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="体检机构名/体检机构" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="queryByCondition()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="体检机构名" align="center"></el-table-column>
                <el-table-column prop="telephone" label="联系电话" align="center"></el-table-column>
                <el-table-column prop="latitude" label="经纬度" align="center"></el-table-column>
                <el-table-column prop="img" label="机构图片" align="center"></el-table-column>
                <el-table-column prop="address" label="详细地址" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button id="handleEdit" type="primary" size="mini" @click="handleUpdate(scope.row)">编辑
                        </el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @sizes-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        :page-sizes="[5,10,15]"
                        layout="total,sizes, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增体检机构地址" :visible.sync="dialogFormVisible">
                    <el-form ref="dataForAdd" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="体检机构名" prop="name">
                                    <el-input v-model="formData.name"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="联系电话" prop="telephone">
                                    <el-input v-model="formData.telephone"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="上传图片">
                                    <el-upload
                                            class="avatar-uploader"
                                            action="/orderaddress/upload.do"
                                            :auto-upload="autoUpload"
                                            name="imgFile"
                                            :show-file-list="false"
                                            :on-success="handleAvatarSuccess"
                                            :before-upload="beforeAvatarUpload">
                                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                    </el-upload>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="经纬度" prop="latitude">
                                    <el-input id='lnglat' type="text" value=""
                                              v-model="formData.latitude" disabled="disabled"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row class="addrow">
                            <el-col :span="12">
                                <el-form-item label="详细地址" prop="address">
                                    <el-input id="tipinput" v-model="formData.address" type="text"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-button id="updateMarker" type="primary" round>设置定位</el-button>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div id="container"></div>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>


            <!--            编辑信息表-->
            <div class="add-form">
                <el-dialog title="编辑预约机构地址" :visible.sync="dialogFormVisible4Edit">
                    <el-form ref="dataForEdit" :model="formData" :rules="rules" label-position="right"
                             label-width="100px">
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="体检机构名" prop="name">
                                    <el-input v-model="formData.name"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-form-item label="联系电话" prop="telephone">
                                    <el-input v-model="formData.telephone"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="24">
                                <el-form-item label="上传图片">
                                    <el-upload
                                            class="avatar-uploader"
                                            action="/orderaddress/upload.do"
                                            :auto-upload="autoUpload"
                                            name="imgFile"
                                            :show-file-list="false"
                                            :on-success="handleAvatarSuccess"
                                            :before-upload="beforeAvatarUpload">
                                        <img v-if="imageUrl" :src="imageUrl" class="avatar">
                                        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
                                    </el-upload>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="经纬度" prop="latitude">
                                    <el-input id='lnglat4edit' type="text" value=""
                                              v-model="formData.latitude" disabled="disabled"></el-input>
                                </el-form-item>
                            </el-col>
                        </el-row>
                        <el-row>
                            <el-col :span="12">
                                <el-form-item label="详细地址" prop="address">
                                    <el-input id="tipinput4edit" v-model="formData.address"></el-input>
                                </el-form-item>
                            </el-col>
                            <el-col :span="12">
                                <el-button id="updateMarker4edit" type="primary" round>设置定位</el-button>
                            </el-col>
                        </el-row>
                    </el-form>
                    <div id="container4edit"></div>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    var vue = new Vue({
        el: '#app',
        data: {
            autoUpload: true,//自动上传
            imageUrl: null,//模型数据，用于上传图片完成后图片预览
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 10,
                total: 100,
                queryString: null,
            },
            rules: {
                // code: [{required: true, message: '请输入编码', trigger: 'blur'}],
                name: [{required: true, message: '请输入名称', trigger: 'blur'}]
            },
            map: null,
            onload: 'onload',
            dataList: [],//列表数据
            formData: {},//表单数据
            addressData: {},
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisible4Edit: false
        },
        created() {
            this.findPage();

        },
        methods: {
            //权限不足提示
            showMessage(r) {
                if (r.response.status == 403) {
                    //权限不足
                    this.$message.error('无访问权限');
                } else {
                    this.$message.error('未知错误');
                }
            },
            //文件上传成功后的钩子，response为服务端返回的值，file为当前上传的文件封装成的js对象
            handleAvatarSuccess(response, file) {
                this.imageUrl = response.data;
                this.$message({
                    message: response.message,
                    type: response.flag ? 'success' : 'error'
                });
                this.formData.img = response.data;
            },
            //上传图片之前执行
            beforeAvatarUpload(file) {
                const isJPG = file.type === 'image/jpeg';
                const isLt2M = file.size / 1024 / 1024 < 2;
                if (!isJPG) {
                    this.$message.error('上传套餐图片只能是 JPG 格式!');
                }
                if (!isLt2M) {
                    this.$message.error('上传套餐图片大小不能超过 2MB!');
                }
                return isJPG && isLt2M;
            },
            //删除
            handleDelete(row) {
                this.$confirm('确定要删除吗', '提示', {type: 'error'})
                    .then(() => {
                        axios.get('/orderaddress/delete.do?id=' + row.id)
                            .then((resp) => {
                                if (resp.data.flag) {
                                    this.$message({
                                        message: resp.data.message,
                                        type: 'success'
                                    });
                                } else {
                                    this.$message.error(resp.data.message);
                                }
                            }).catch((r) => {
                            this.showMessage(r)
                        }).finally(() => {
                            this.findPage();
                        })
                    }).catch(() => {
                    this.$message({
                        message: '删除已取消',
                        type: 'error'
                    });
                })
            },
            destroyMap() {
                this.map && this.map.destroy();
                console.log(vue.map)
                console.log('地图一小会')
            },
            //弹出编辑窗口
            handleUpdate(row) {
                this.destroyMap();
                this.dialogFormVisible4Edit = true;
                /*this.formData.latitude = row.latitude;
                console.log('vvv'+this.formData.latitude)
                this.formData.address = row.address*/
                this.$nextTick(() => {
                    this.$refs['dataForEdit'].resetFields();
                    this.initMap4edit(row);
                });

                // this.controllId = row.id;
                axios.get('/orderaddress/findById.do?id=' + row.id)
                    .then((resp) => {
                        if (resp.data.flag) {
                            this.formData = resp.data.data;
                            this.imageUrl = resp.data.data.img;
                            /*axios.post('/setmeal/findImgUrl.do', this.formData)
                                .then((resp) => {
                                    if (resp.data.flag) {
                                        this.imageUrl = resp.data.data;
                                    } else {
                                        this.$message({
                                            message: resp.data.message,
                                            type: 'error'
                                        });
                                    }
                                })*/
                        } else {
                            this.$message({
                                message: resp.data.message,
                                type: 'error'
                            });
                        }
                    });


            },
            //编辑
            handleEdit() {
                this.$refs['dataForEdit'].validate((valid) => {
                    if (valid) {
                        axios.post('/orderaddress/edit.do', this.formData)
                            .then((resp) => {
                                if (resp.data.flag) {
                                    this.dialogFormVisible4Edit = false;
                                    this.$message({
                                        message: resp.data.message,
                                        type: 'success'
                                    });
                                    this.findPage();
                                } else {
                                    this.$message({
                                        message: resp.data.message,
                                        type: 'error'
                                    });
                                }
                            })
                    } else {
                        this.$message({
                            message: '数据校验失败，请检查后重试',
                            type: 'error'
                        });
                    }
                })

            },
            //添加
            handleAdd() {
                this.$refs['dataForAdd'].validate((valid) => {
                    if (valid) {
                        axios.post('/orderaddress/add.do', this.formData)
                            .then((resp) => {
                                if (resp.data.flag) {
                                    this.dialogFormVisible = false;
                                    this.$message({
                                        message: resp.data.message,
                                        type: 'success'
                                    });
                                    this.findPage();
                                } else {
                                    this.$message({
                                        message: resp.data.message,
                                        type: 'error'
                                    });
                                }
                            })
                    } else {
                        this.$message({
                            message: '数据校验失败，请检查后重试',
                            type: 'error'
                        });
                    }
                })
            },
            //分页查询
            findPage() {
                let pageInfo = {
                    currentPage: this.pagination.currentPage,
                    pageSize: this.pagination.pageSize,
                    queryString: this.pagination.queryString
                }
                axios.post('/orderaddress/findByCondition.do', pageInfo)
                    .then((resp) => {
                        this.pagination.total = resp.data.total;
                        this.dataList = resp.data.rows;
                    })
            },
            //根据条件查询，重置页码
            queryByCondition() {
                this.pagination.currentPage = 1;
                this.findPage();
            },
            // 重置表单
            resetForm() {
                this.$nextTick(() => {
                    this.$refs['dataForAdd'].resetFields();
                    this.formData = {};
                    this.imageUrl = null;
                })
            },
            // 弹出添加窗口
            handleCreate() {

                this.dialogFormVisible = true;
                this.$nextTick(() => {
                    this.$refs['dataForAdd'].resetFields();
                    this.formData = {};
                    this.imageUrl = null;
                    this.initMap();
                })
                // this.initMap();
            },
            //切换页码
            handleCurrentChange(currentPage) {
                this.pagination.currentPage = currentPage;
                this.findPage();
            },
            //切换页面显示条数
            handleSizeChange(pageSize) {
                this.pagination.pageSize = pageSize;
                this.findPage();
            },
            //初始化地图
            initMap() {
                var url = 'http://webapi.amap.com/maps?v=1.4.15&key=2cf5fcd8c3c0f969590834a513776a95';
                console.log(url)

                /*var jsapi = document.createElement('script');
                jsapi.charset = 'utf-8';
                jsapi.src = url;
                document.head.appendChild(jsapi);*/
                //初始化地图
                // window[this.onload] = function () {
                var map = window.map = new AMap.Map('container', {
                    resizeEnable: true,
                    zoom: 10,//级别
                    // center: [116.397428, 39.90923],//中心点坐标
                    viewMode: '3D'//使用3D视图
                });
                // initToolbar();
                //输入提示
                // AMap.plugin(['AMap.Autocomplete', 'AMap.PlaceSearch'], function () {
                var autoOptions = {
                    input: 'tipinput'
                };
                var auto = new AMap.Autocomplete(autoOptions);
                var placeSearch = new AMap.PlaceSearch({
                    map: map,
                    panel: "panel", // 结果列表将在此容器中进行展示。
                });  //构造地点查询类
                AMap.event.addListener(auto, "select", select);//注册监听，当选中某条记录时会触发
                function select(e) {
                    console.log('yyyyy')
                    placeSearch.setCity(e.poi.adcode);
                    placeSearch.search(e.poi.name);  //关键字查询查询
                    geoCode();
                }

                // })
                //地址转坐标
                var geocoder = new AMap.Geocoder();

                var marker = new AMap.Marker();

                function geoCode() {
                    var address = document.getElementById('tipinput').value;
                    console.log(address)
                    vue.formData.address = address;
                    console.log(vue.formData.address)
                    geocoder.getLocation(address, function (status, result) {
                        if (status === 'complete' && result.geocodes.length) {
                            var lnglat = result.geocodes[0].location
                            document.getElementById('lnglat').value = lnglat;
                            vue.formData.latitude = document.getElementById('lnglat').value;
                            console.log(vue.formData.latitude)
                            //初始化标记图标
                            marker.setIcon("//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png")
                            //设置图标
                            marker.setPosition(lnglat);
                            map.add(marker);
                            map.setFitView(marker);
                        } else {
                            log.error('根据地址查询位置失败');
                        }
                    });
                }

                document.getElementById('tipinput').onkeydown = function (e) {
                    if (e.keyCode === 13) {
                        geoCode();
                        return false;
                    }
                    return true;
                };

                document.getElementById('updateMarker').onclick = function updateIcon() {
                    marker.setIcon('//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-red.png')
                }

            },
            //编辑初始化地图
            initMap4edit(row) {
                var url = 'http://webapi.amap.com/maps?v=1.4.15&key=2cf5fcd8c3c0f969590834a513776a95';
                console.log(url)
                console.log(document.getElementById('tipinput4edit').value)
                // let latitude = vue.formData.latitude;
                let latitude = row.latitude;
                let arr = new Array();
                if (latitude) {
                    arr = latitude.split(',');
                }
                var longitudeNum = parseFloat(arr[0]);
                var latitudeNum = parseFloat(arr[1]);
                let posArr = new Array();
                posArr.push(longitudeNum);
                posArr.push(latitudeNum);

                /*var jsapi = document.createElement('script');
                jsapi.charset = 'utf-8';
                jsapi.src = url;
                document.head.appendChild(jsapi);*/
                //初始化地图
                // window[this.onload] = function () {
                var map = vue.map = new AMap.Map('container4edit', {
                    resizeEnable: true,
                    zoom: 100,//级别
                    center: posArr,//中心点坐标
                    viewMode: '3D'//使用3D视图
                });

                //输入提示
                AMap.plugin(['AMap.Autocomplete', 'AMap.PlaceSearch'], function () {
                    var autoOptions = {
                        input: 'tipinput4edit'
                    };
                    var auto = new AMap.Autocomplete(autoOptions);
                    var placeSearch = new AMap.PlaceSearch({
                        map: map
                    });  //构造地点查询类
                    AMap.event.addListener(auto, "select", select);//注册监听，当选中某条记录时会触发
                    function select(e) {
                        console.log('yyyyy')
                        placeSearch.setCity(e.poi.adcode);
                        placeSearch.search(e.poi.name);  //关键字查询查询
                        geoCode();
                    }
                })

                // 启用工具条
                AMap.plugin(['AMap.ToolBar'], function () {
                    map.addControl(new AMap.ToolBar({
                        position: 'RB'
                    }))
                })
                //地址转坐标
                var geocoder = new AMap.Geocoder();

                console.log(posArr);

                var marker = new AMap.Marker({
                    icon: "//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png",
                    position: posArr,
                    offset: new AMap.Pixel(-13, -30)
                });
                marker.setMap(map);

                function geoCode() {
                    var address = document.getElementById('tipinput4edit').value;
                    vue.formData.address = address;
                    geocoder.getLocation(address, function (status, result) {
                        if (status === 'complete' && result.geocodes.length) {
                            var lnglat = result.geocodes[0].location
                            document.getElementById('lnglat4edit').value = lnglat;
                            vue.formData.latitude = document.getElementById('lnglat4edit').value;
                            //初始化标记图标
                            marker.setIcon("//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png")
                            //设置图标
                            marker.setPosition(lnglat);
                            map.add(marker);
                            map.setFitView(marker);
                        } else {
                            log.error('根据地址查询位置失败');
                        }
                    });
                }

                // geoCode();
                document.getElementById('tipinput4edit').onkeydown = function (e) {
                    if (e.keyCode === 13) {
                        geoCode();
                        return false;
                    }
                    return true;
                };
                /*document.getElementById('updateMarker4edit').onclick = function updateIcon() {
                    geoCode();
                    marker.setIcon('//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-red.png')
                }*/
                document.getElementById('updateMarker4edit').onclick = function updateIcon() {
                    var address = document.getElementById('tipinput4edit').value;
                    vue.formData.address = address;
                    geocoder.getLocation(address, function (status, result) {
                        if (status === 'complete' && result.geocodes.length) {
                            var lnglat = result.geocodes[0].location
                            document.getElementById('lnglat4edit').value = lnglat;
                            vue.formData.latitude = document.getElementById('lnglat4edit').value;
                            //初始化标记图标
                            marker.setIcon('//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-red.png')
                            //设置图标
                            marker.setPosition(lnglat);
                            map.add(marker);
                            map.setFitView(marker);
                        } else {
                            log.error('根据地址查询位置失败');
                        }
                    });
                }
            }


            // }
        }
    })
</script>
</html>
